package com.ccn.ui.a1ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 高级绘制
 * Paint,Canvas,Path详解
 *
 * 1.2.1见A1_1UIPaintFlowPath
 *
 * 1.2.2Pain-路径 XFERMODE
 * 混合效果只作用于src源图像区域
 * //注 黄色圆为目标图  蓝色方块为源图 图片：xformode
 * //1所绘制不会提交到原图上，即源图部分全部透明
 * new PorterDuffXfermode(PorterDuff.Mode.CLEAR),
 * //2显示上层绘制的图像
 * new PorterDuffXfermode(PorterDuff.Mode.SRC),
 * //3显示下层绘制的图像
 * new PorterDuffXfermode(PorterDuff.Mode.DST),
 * //4正常绘制显示，源图在上，盖住其他
 * new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER),
 * //5正常绘制显示，目标图在上，盖住其他
 * new PorterDuffXfermode(PorterDuff.Mode.DST_OVER),
 * //6取绘制交集，显示源图，即源图交集部分显示，其余部分透明
 * new PorterDuffXfermode(PorterDuff.Mode.SRC_IN),
 * //7取绘制交集，显示目标层，即源图全部透明
 * new PorterDuffXfermode(PorterDuff.Mode.DST_IN),
 * //8源图绘制交集部分透明
 * new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT),
 * //9目标图绘制交集透明，源图全部透明
 * new PorterDuffXfermode(PorterDuff.Mode.DST_OUT),
 * //10交集部分，源图在上，即源图仅显示交集部分
 * new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP),
 * //11交集部分，目标层在上，即源图不显示交集部分
 * new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP),
 * //12去除两层交集部分
 * new PorterDuffXfermode(PorterDuff.Mode.XOR),
 * //13交集部分颜色加深
 * new PorterDuffXfermode(PorterDuff.Mode.DARKEN),
 * //14交集部分颜色点亮
 * new PorterDuffXfermode(PorterDuff.Mode.LIGHTEN),
 * //15交集部分颜色叠加，即源图仅显示交集部分
 * new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY),
 * //16交集部分滤色，即源图交集部分滤色，其余部分正常
 * new PorterDuffXfermode(PorterDuff.Mode.SCREEN),
 * //17取两图全部区域，交集部分饱和度相加
 * new PorterDuffXfermode(PorterDuff.Mode.ADD),
 * //18取两图全部区域，交集部分叠加
 * new PorterDuffXfermode(PorterDuff.Mode.OVERLAY),
 */
public class A1_2PaintCanvasSeniorDraw extends View {

    private Paint mPaint;
    private int mWidth;
    private int mHeight;
    public A1_2PaintCanvasSeniorDraw(Context context) {
        this(context,null);
    }

    public A1_2PaintCanvasSeniorDraw(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public A1_2PaintCanvasSeniorDraw(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //初始化画笔
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //图层混合三种方式
        //1.ComposeShader
        //2.画笔Paint.setXfermode() 本节主讲
        //3.PorterDuffColorFiler

        //禁止硬件加速  图层混合某些api不支持硬件加速
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);

        setBackgroundColor(Color.GRAY);

        //离屏绘制 如果不使用离屏绘制，将导致背景参与混合
        int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(),
                null,
                Canvas.ALL_SAVE_FLAG
        );
        //目标图
        canvas.drawBitmap(makeDst(mWidth,mHeight), 0, 0, mPaint);
        //设置混合模式
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        //源图，层叠区域右下角部分
        canvas.drawBitmap(makeSrc(mWidth,mHeight), 0, 0, mPaint);
        //清除混合模式
        mPaint.setXfermode(null);
        canvas.restoreToCount(sc);
    }


    // create a bitmap with a circle, used for the "dst" image
    static Bitmap makeDst(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

        p.setColor(0xFFFFCC44);
        c.drawOval(new RectF(0, 0, w*3/4, h*3/4), p);
        return bm;
    }

    // create a bitmap with a rect, used for the "src" image
    static Bitmap makeSrc(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);

        p.setColor(0xFF66AAFF);
        c.drawRect(w/3, h/3, w*19/20, h*19/20, p);
        return bm;
    }
}
